<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 18/3/26
 * Time: 16:45
 */

namespace App\Services\Admin;


use App\Models\Account;
use App\Models\User;
use App\Models\UserWallet;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Laravel\Passport\Bridge\AccessTokenRepository;
use Laravel\Passport\Bridge\Client;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
class UserAccountService
{
    use ChartTrait;
    protected $repo;
    protected $model;
    public function __construct()
    {
        $this->model = new Account();
    }
    public function lists(Request $request){
        $model = $this->model;
        $keywords = $request->input('keywords','');
        $date_range = $request->input('timeRange','');
        $type = $request->input('type',-1);
        $date_range = explode(',',$date_range);
        $model = $model->with('user')->orderBy('created_at','desc');

        if(count($date_range)==2){
            $model = $model->whereBetween('created_at',$date_range);
        }
        if($type>0){
            $model = $model->where('type',$type);
        }
        if($keywords){
            $userModel = new User();
            $ids = $userModel->where('name','like','%'.$keywords.'%')
                ->orWhere('mobile','like','%'.$keywords.'%')
                ->orWhere('remark','like','%'.$keywords.'%')
                ->pluck('id');
            $model = $model->when($ids,function($query) use($ids){
                return $query->whereIn('user_id',$ids);
            });
        }
        return $model->paginate(10);
    }
    public function getAccount($id){
        $model = new User();
        return $model->with('wallet')->where('id','>',1)->find($id);
    }
    public function count($type,$day_type='all'){
        $model = $this->model;
        $count = 0;
        switch ($day_type){
            case 'yesterday':
                $model = $model->whereBetween('created_at',[Carbon::yesterday(),Carbon::today()]);
                break;
        }
        switch ($type){
            case 'cost':
                $model = $model->where('type',Account::TYPE_COST);
                break;
            case 'charge':
                $model = $model->where('type',Account::TYPE_CHARGE);
                break;
        }
        $count = $model->sum('amount');
        return $count;
    }
    public function getCharts($date_type,$type=Account::TYPE_CHARGE){
        $groupBy = $this->getGroupBy($date_type);
        $model = $this->model;
        $model = $model->where('user_id','<>',1);
        $model = $model->where('type',$type);
        if($type==Account::TYPE_COST){
            $model = $model->select(DB::raw('abs(sum(amount)) as _res,'.$groupBy.'as _time'));

        }else{
            $model = $model->select(DB::raw('sum(amount) as _res,'.$groupBy.'as _time'));

        }
        $timeRange = $this->getDateRange($date_type);
        $model = $model->whereBetween('created_at',$timeRange);
        $model = $model->groupBy(DB::raw($groupBy));
        $model = $model->orderBy('_time','ASC');
        $data = $model->get();
        return $this->parseData($data,$date_type);
    }
}